﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPHelper
{
	public class View
	{
		/// <summary>
		/// Update the properties of the view passed
		/// </summary>
		/// <param name="view"></param>
		/// <param name="isPaginate"></param>
		/// <param name="numberItems"></param>
		/// <returns></returns>
        //public static bool UpdateView(SPView view, bool isPaginate, int? numberItems, 
        //    List<Caml.Predicate> camlPredicates, List<Caml.OrderByField> camlOrderByFields)
        //{
        //    bool isSuccessful = false;
        //    try
        //    {
        //        // init
        //        if (view == null) { throw new Exception("SPView passed is null"); }
        //        int defaultNumberItems = 30;
        //        if (numberItems == null) { numberItems = defaultNumberItems; }
        //        if (numberItems != null && numberItems > 2147483646) { throw new Exception("Row Limit for a view must be less than 2147483647"); }

        //        // paginate?
        //        view.Paged = isPaginate;

        //        // number of items
        //        view.RowLimit = Convert.ToUInt32(numberItems);

        //        // sort & filter
        //        string camlQuery = null;
        //        if (camlPredicates != null && camlPredicates.Count > 0)
        //        {
        //            //camlQuery += Caml.FormatPredicates(camlPredicates);
        //        }
        //        if (camlOrderByFields != null && camlOrderByFields.Count > 0)
        //        {
        //            camlQuery += Caml.FormatOrderBy(camlOrderByFields); 
        //        }
        //        if (!string.IsNullOrEmpty(camlQuery)) { view.Query = camlQuery; }

        //        // save changes to view
        //        view.Update();

        //        // save list as well
        //        // view.ParentList.Update();

        //        // assume success
        //        isSuccessful = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        SPHelper.Log.WriteError("fgyjkugjfgrtdrfcxvcxzdfsuygf", ex);
        //        isSuccessful = false;
        //    }
        //    return isSuccessful;
        //}

        //public static bool UpdateView(SPView view, bool isPaginate, int? numberItems)
        //{
        //    return UpdateView(view, isPaginate, numberItems, null, null);
        //}


		/// <summary>
		/// update the default view properties for ALL lists in the passed web
		/// But only if the list has a default view
		/// </summary>
		/// <param name="web"></param>
		/// <param name="isPaginate"></param>
		/// <param name="numberItems"></param>
		/// <returns></returns>
        //public static bool UpdateAllListDefaultViews(SPWeb web, bool isPaginate, int? numberItems,
        //    List<Caml.Predicate> camlPredicates, List<Caml.OrderByField> camlOrderByFields)
        //{
        //    bool isSuccessful = true;
        //    try
        //    {
        //        // init
        //        if (web == null) { throw new Exception("SPWeb passed is null"); }


        //        for (int i = 0; i < web.Lists.Count; i++)
        //        {
        //            try
        //            {
        //                if (web.Lists[i].DefaultView != null)
        //                {
        //                    bool _isSuccessful = UpdateView(web.Lists[i].DefaultView, isPaginate, numberItems,
        //                        camlPredicates, camlOrderByFields);
        //                    if (!_isSuccessful && isSuccessful) { isSuccessful = _isSuccessful; }
        //                }
        //            }
        //            catch (Exception _ex)
        //            {
        //                SPHelper.Log.WriteError("ytuygugddfsdsfdsf", _ex);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        SPHelper.Log.WriteError("dtyioiukhvvncfgdstr", ex);
        //        isSuccessful = false;

        //    }
        //    return isSuccessful;
        //}

        ///// <summary>
        ///// update the default view properties for ALL lists in the passed web
        ///// But only if the list has a default view
        ///// </summary>
        ///// <param name="web"></param>
        ///// <param name="isPaginate"></param>
        ///// <param name="numberItems"></param>
        ///// <returns></returns>
        //public static bool UpdateAllListDefaultViews(SPWeb web, bool isPaginate, int? numberItems)
        //{
        //    return UpdateAllListDefaultViews(web, isPaginate, numberItems, null, null);
        //}

        //public static bool create()
        //{
        //    string viewName = "Calendar";
        //    string query = "<Where><DateRangesOverlap><FieldRef Name=\"StartDate\"/><FieldRef Name=\"_EndDate\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where>";
        //    bool paged = true;
        //    bool defaultview = false;
        //    bool personalView = false;
        //    //list.Views.Add(viewName, viewFields, query, Int32.MaxValue, paged, defaultview, SPViewCollection.SPViewType.Calendar, personalView);
        //}
	}
}
